import 'package:flutter/material.dart';
import 'package:xws/helpers/safe_state.dart';
import 'package:xws/models/user/user_obj.dart';
import 'package:xws/utils/app_util.dart';
import 'package:xws/utils/dio_utils/request/user_req.dart';
import 'package:xws/utils/theme_util.dart';
import 'package:xws/widgets/app_btn.dart';
import 'package:xws/widgets/app_img.dart';
import 'package:xws/widgets/app_list_tile.dart';
import 'package:xws/widgets/refresh_widget/refresh_load_view.dart';
import 'package:xws/widgets/safe_scaffold.dart';

/// 黑名单
class BlackList extends StatefulWidget {
  const BlackList({Key? key}) : super(key: key);

  @override
  _BlackListState createState() => _BlackListState();
}

class _BlackListState extends State<BlackList> with SafeState<BlackList> {
  late RefreshViewController<User> _viewController;

  @override
  void initState() {
    super.initState();
    _viewController = RefreshViewController<User>();
  }

  @override
  void dispose() {
    _viewController.dispose();
    super.dispose();
  }

  /// 点击用户尾部按钮
  Future<void> _onTap(User u) async {
    loading(context);

    final bool r = await unBlackUser(u.id);
    if (r) await _viewController.refresh();

    close(context);
  }

  @override
  Widget build(BuildContext context) {
    return SafeScaffold(titleText: '黑名单', body: _buildList);
  }

  Widget get _buildList {
    return ColoredBox(
      color: white(context),
      child: RefreshLoadView<User>(
        viewController: _viewController,
        onRefresh: (int v, _) async {
          return await getBlackList(pageNum: 1);
        },
        onLoad: (int page, _) async {
          return await getBlackList(pageNum: page);
        },
        itemBuilder: (int index, User u) => _buildItem(u),
      ),
    );
  }

  Widget _buildItem(User u) {
    return AppListTile(
      height: 64,
      padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 10),
      leading: Padding(
        padding: const EdgeInsets.only(right: 10),
        child: AppImg.fitCover(
          url: u.avatarUrl,
          width: 44,
          height: 44,
          radius: 30,
        ),
      ),
      title: Padding(
        padding: const EdgeInsets.only(right: 10),
        child: Row(
          children: <Widget>[
            Container(
              child: Text(
                u.city,
                style: TextStyle(
                  fontSize: 10,
                  color: white(context),
                  height: 1.19,
                ),
              ),
              height: 17,
              padding: const EdgeInsets.symmetric(horizontal: 3),
              alignment: Alignment.center,
              margin: const EdgeInsets.only(right: 4),
              decoration: BoxDecoration(
                color: theme(context),
                borderRadius: BorderRadius.circular(2),
              ),
            ),
            Flexible(
              child: Text(
                u.nickName,
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                style: TextStyle(
                  fontSize: 14,
                  fontWeight: FontWeight.bold,
                  color: black(context).withOpacity(0.6),
                ),
              ),
            ),
          ],
        ),
      ),
      subtitle: Text(
        u.description,
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
        style: TextStyle(
          fontSize: 12,
          color: black(context).withOpacity(0.6),
        ),
      ),
      trailing: AppBtn(
        onTap: () => _onTap(u),
        text: '移除',
        textStyle: TextStyle(
          color: theme(context),
        ),
        color: Colors.transparent,
        border: Border.all(color: theme(context)),
      ),
    );
  }
}
